import ajax from './ajax'
import jsonp from 'jsonp'
import {message} from 'antd'
// export default{
//    xxx(){

//    },
//    yyy(){

//    }
// }

// 登录
// export function reqLogin(username,password){
//    return ajax('/login',{username,password},'POST')
// }


const BASE = ''

export const reqLogin = (username,password)=> ajax(BASE+'/login',{username,password},'POST')


// jsonp 请求接口函数  其他接口都是返回 promise 对象  所以这里也要包装成  promise 对象  不是 Ajax请求
export const reqWeather = (city)=>{
    return new Promise((resolve,reject)=>{
        //const url = `http://api.map.baidu.com/telematics/v3/weather?location=${city}&output=json&ak=3p49MVra6urFRGOT9s8UBWr2`
        const url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${city}&key=1936045b6809a07e0906e2ec858ec326`
        jsonp(url,{},(err,data)=>{
            //  if(!err && data.status==='success'){
            //    // 取出需要的数据
            //    const {dayPictureUrl, weather} = data.results[0].weather_data[0]
            //    resolve(dayPictureUrl,weather)
            //  }else{
            //      // 如果失败了
            //      message.error('获取天气信息失败!')
            //  }
            if(!err && data.status==='1'){
                //const { weather} = data.lives[0].weather
                //console.log('weather',data.lives[0].weather);
                resolve(data.lives[0].weather)
            }else{
                 // 如果失败了
                 message.error('获取天气信息失败!')
            } 
        })
    })
}
//reqWeather('110101')
// reqWeather('北京')

// 获取 一级/二级分类列表
export const reqCategorys = (parentId)=> ajax(BASE+'/manage/category/list',{parentId:parentId})



// 添加一级分类
export const reqAddCategory = (categoryName,parentId)=> ajax(BASE+'/manage/category/add',{categoryName,parentId},'POST')


// 更新 一级分类
export const reqUpdateCategory = ({categoryName,categoryId})=> ajax(BASE+'/manage/category/update',{categoryName,categoryId},'POST')


// 获取商品分页列表   (pageNum, pageSize) 这样要求 传参的顺序一致  参数名字可以随便
export const reqProducts = (pageNum, pageSize) => ajax(BASE+'/manage/product/list', {pageNum, pageSize})


/*
搜索商品分页列表 (根据商品名称/商品描述)
searchType: 搜索的类型, productName/productDesc
 */
export const reqSearchProducts = ({pageNum, pageSize, searchName, searchType}) => ajax(BASE+'/manage/product/search', {
    pageNum,
    pageSize,
    [searchType]: searchName,
  })

// 搜索商品分页列表 (根据商品描述)  ({pageNum, pageSize, searchName}) 要求外部传的参数 名字只能是 pageNum, pageSize, searchName  
/*export const reqSearchProducts2 = ({pageNum, pageSize, searchName}) => ajax(BASE + '/manage/product/search', {
  pageNum,
  pageSize,
  productName: searchName,
})
搜索商品分页列表 (根据商品名称)
export const reqSearchProducts1 = ({pageNum, pageSize, searchName}) => ajax(BASE + '/manage/product/search', {
  pageNum,
  pageSize,
  productDesc: searchName,
})
*/


// 获取一个分类  详情页
export const reqCategory = (categoryId) => ajax(BASE+'/manage/category/info', {categoryId})


// 更新商品的状态(上架/下架)
export const reqUpdateStatus = (productId, status) => ajax(BASE+'/manage/product/updateStatus', {productId, status}, 'POST')


// 删除指定名称的图片
export const reqDeleteImg = (name) => ajax(BASE+'/manage/img/delete', {name}, 'POST')



// 添加/修改商品
export const reqAddOrUpdateProduct = (product) => ajax(BASE+'/manage/product/' + ( product._id?'update':'add'), product, 'POST')

// 添加商品
// export const reqAddProduct = (product) => ajax(BASE + '/manage/product/add', product, 'POST')
// 修改商品
// export const reqUpdateProduct = (product) => ajax(BASE + '/manage/product/update', product, 'POST')


// 获取所有角色的列表
export const reqRoles = () => ajax(BASE+'/manage/role/list')


// 添加角色  形参 roleName
export const reqAddRole = (roleName) => ajax(BASE+'/manage/role/add', {roleName}, 'POST')

// 更新角色  role 本身是一个对象  不要 {}
export const reqUpdateRole = (role) => ajax(BASE+'/manage/role/update', role, 'POST')



// 获取所有用户的列表
export const reqUsers = () => ajax(BASE+'/manage/user/list')



// 删除指定用户
export const reqDeleteUser = (userId) => ajax(BASE+'/manage/user/delete', {userId}, 'POST')


// 添加/更新用户  user 自身是个对象
export const reqAddOrUpdateUser = (user) => ajax(BASE+'/manage/user/'+(user._id ? 'update' : 'add'), user, 'POST')